package com.xs.designs.strategy.factory.template_method.III;

import com.xs.designs.strategy.factory.template_method.I.PaymentStrategy;
import com.xs.designs.strategy.factory.template_method.II.AliPay;
import com.xs.designs.strategy.factory.template_method.II.UnionPay;
import com.xs.designs.strategy.factory.template_method.II.WeChatPay;
import org.springframework.stereotype.Service;

@Service
public class PaymentContext {
    private PaymentStrategy paymentStrategy;

    public void setPaymentStrategy(int payType) {
        switch (payType) {
            case 1:
                this.paymentStrategy = new WeChatPay();
                break;
            case 2:
                this.paymentStrategy = new AliPay();
                break;
            case 3:
                this.paymentStrategy = new UnionPay();
                break;
            default:
                throw new IllegalArgumentException("无效的支付类型");
        }
    }

    public void pay(double amount) {
        if (paymentStrategy == null) {
            throw new IllegalStateException("支付策略未设置");
        }
        paymentStrategy.pay(amount);
    }
}
